﻿using System;
using System.Linq;
using EPiServer.Core;
using NUnit.Framework;
using EPiServer;
using NUnit.Framework.SyntaxHelpers;
using EPiServer.DataAccess;
using EPiServer.Security;
using System.Security.Principal;

namespace EPiSugar.Tests.UnitTesting
{
    [TestFixture]
    public class FakePageProviderTests : FakingEPiServerTests
    {
        private IPrincipal originalPrincipal;
        [TestFixtureSetUp]
        public override void TestFixtureSetUp()
        {
            base.TestFixtureSetUp();

            originalPrincipal = PrincipalInfo.CurrentPrincipal;
            PrincipalInfo.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Administrator"), new[] { "Administrators", "Everyone" });
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            PrincipalInfo.CurrentPrincipal = originalPrincipal;
        }



        [Test]
        public void CanGetRootPage()
        {
            PageData rootPage = DataFactory.Instance.GetPage(PageReference.RootPage);
            Assert.That(rootPage, Is.Not.Null);
        }

        [Test]
        public void CanGetStartPage()
        {
            PageData startPage = DataFactory.Instance.GetPage(PageReference.StartPage);
            Assert.That(startPage, Is.Not.Null);
        }

        [Test]
        public void CanGetChildren()
        {
            var children = DataFactory.Instance.GetChildren(PageReference.RootPage);
            Assert.That(children.Count, Is.EqualTo(2));
        }

        [Test]
        public void CanUpdate()
        {
            PageData startPage = DataFactory.Instance.GetPage(PageReference.StartPage);

            startPage.PageName += " 2";
            DataFactory.Instance.Save(startPage, SaveAction.Publish);

            startPage = DataFactory.Instance.GetPage(PageReference.StartPage);
            Assert.That(startPage.PageName, Is.EqualTo("Start Page 2"));
        }

        [Test]
        public void CanMove()
        {
            PageData sourcePage = DataFactory.Instance.GetChildren(PageReference.StartPage).ElementAt(1);
            PageData destinationPage = DataFactory.Instance.GetChildren(PageReference.StartPage).First();
            
            DataFactory.Instance.Move(sourcePage.PageLink, destinationPage.PageLink);

            sourcePage = DataFactory.Instance.GetPage(sourcePage.PageLink);
            Assert.That(sourcePage.ParentLink, Is.EqualTo(destinationPage.PageLink));
            var pages = DataFactory.Instance.GetChildren(destinationPage.PageLink);
            Assert.That(pages.Where(p => p.PageLink == sourcePage.PageLink).Count(), Is.GreaterThan(0));
        }

        [Test]
        public void CanCopy()
        {
            PageData destinationPage = DataFactory.Instance.GetChildren(PageReference.StartPage).First();
            PageData sourcePage = DataFactory.Instance.GetChildren(PageReference.StartPage).ElementAt(1);

            var copyLink = DataFactory.Instance.Copy(sourcePage.PageLink, destinationPage.PageLink);

            PageData copyPage = DataFactory.Instance.GetPage(copyLink);
            Assert.That(copyPage.PageLink, Is.Not.EqualTo(sourcePage.PageLink)); 
            Assert.That(copyPage.ParentLink, Is.EqualTo(destinationPage.PageLink));
            var pages = DataFactory.Instance.GetChildren(destinationPage.PageLink);
            Assert.That(pages.Where(p => p.PageName == copyPage.PageName).Count(), Is.GreaterThan(0));
        }

        [Test]
        public void CanDelete()
        {
            int initialCount = DataFactory.Instance.GetChildren(PageReference.StartPage).Count;

            PageData page = DataFactory.Instance.GetChildren(PageReference.StartPage).First();
            DataFactory.Instance.Delete(page.PageLink, true);

            int afterCount = DataFactory.Instance.GetChildren(PageReference.StartPage).Count;
            Assert.That(afterCount, Is.EqualTo(initialCount - 1));
        }
    }
}
